﻿Imports ISNet.WebUI.WebGrid
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports log4net


Partial Class mtCmmtDetail
    Inherits System.Web.UI.Page

    Public AlertText, ScriptText As String
    Private Shared logger As ILog = LogManager.GetLogger("mtCmmtDetail")

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        ScreenName.Text = "ระบบจัดทำงบประมาณ  >>  ตั้งค่าระบบ >> CommitmentItem Detail"

        If Session("users") Is Nothing Then
            Response.Redirect("LoginForm.aspx")
        End If
        
        If IsPostBack = False Then

            PlanYearsTxt.Text = Session("PlanYear")
            Session("PlanYearTxt") = PlanYearsTxt.Text
            Session("SearchTxt") = ""
            Session("SearchTxt") = " and PlanYear = " & PlanYearsTxt.Text
        End If

    End Sub

    Protected Sub WebGrid1_InitializeDataSource(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.DataSourceEventArgs) Handles WebGrid1.InitializeDataSource
        If Session("SearchTxt") = "" Then
            Exit Sub
        End If
        Dim adapter As New MasterDataSetTableAdapters.CmmtDetailsTableAdapter
        Dim dtTable As New MasterDataSet.CmmtDetailsDataTable

        Dim sql As String = "select * from CmmtDetails "
        sql += " WHERE 1 = 1 "

        If TabContainer1.ActiveTab.ID = TabPanel1.ID Then
            If CommtItemCombo.Value <> "" Then
                sql += Session("SearchTxt")
            End If
        End If

        If TabContainer1.ActiveTab.ID = TabPanel2.ID Then
            If F09ProductWebCombo.Value <> "" Then
                sql += Session("SearchTxt")
            End If
        End If

        If TabContainer1.ActiveTab.ID = TabPanel3.ID Then
            If F10ProductWebCombo.Value <> "" Then
                sql += Session("SearchTxt")
            End If
        End If

        sql += " ORDER BY itemNo"
        adapter.SearchCmmtDetail(dtTable, sql)
        'Response.Write(sql)
        Session("count_data") = dtTable.Rows.Count



        e.DataSource = dtTable

    End Sub

    Protected Sub WebGrid1_InitializeRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        ' don't edit column
        ' e.Row.Cells(0).ForceNoEdit = True
        e.Row.Cells(5).ForceNoEdit = True

        Dim lb As New Label
        lb = CType(e.Row.Cells.GetNamedItem("DesShow").GetTemplateContainer().FindControl("L_DesShow"), Label)
        lb.Text = GetShowDes(e.Row.KeyValue)

        Dim lb_enter As New Label
        lb_enter = CType(e.Row.Cells.GetNamedItem("EnterAmountFlag").GetTemplateContainer().FindControl("L_EnterAmountFlag"), Label)
        lb_enter.Text = getEnterFlag(e.Row.KeyValue)


    End Sub

    Protected Sub WebGrid1_InitializePostBack(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.PostbackEventArgs)
        If e.Action = PostBackAction.Custom Then
            Dim type As String = TryCast(Request("type"), String)
            Dim Deletet As String = TryCast(Request("id"), String)
            Dim adapter As New MasterDataSetTableAdapters.CmmtDetailsTableAdapter

            ' MsgBox(Deletet)
            If type = "1" Then
                Try
                    adapter.DeleteCmmtDetail(Deletet)
                Catch ex As Exception
                    WebGrid1.ClientAction.Alert("ไม่สามารลบข้อมูลได้กรุณาติดต่อผู้ดูแลระบบ")
                End Try
            ElseIf type = "2" Then
                Try
                    For Each keyValue As String In WebGrid1.RootTable.GetCheckedRows()
                        adapter.DeleteCmmtDetail(keyValue)
                    Next keyValue
                Catch ex As Exception
                    WebGrid1.ClientAction.Alert("ไม่สามารลบข้อมูลได้กรุณาติดต่อผู้ดูแลระบบ")
                End Try

            End If
            WebGrid1.ClientAction.Refresh()
        End If
    End Sub

    Protected Sub SearhBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SearhBtn.Click
        'If CommtItemCombo.Value = "" Then
        '    Alert("กรุณาเลือก Commitment Item")
        '    Exit Sub
        'End If
        Session("SearchTxt") = ""
        If TabContainer1.ActiveTab.ID = TabPanel1.ID Then
            If CommtItemCombo.Value <> "" Then
                Session("SearchTxt") += " and Cmmt_CmmtCode = '" & CommtItemCombo.Value & "'"
                Session("SearchTxt") += " and Cmmt_PlanYear = " & PlanYearsTxt.Text
            Else
                Alert("กรุณาเลือก Commitment Item")
                Exit Sub
            End If
        ElseIf TabContainer1.ActiveTab.ID = TabPanel2.ID Then
            If F09ProductWebCombo.Value <> "" Then
                'Session("SearchTxt") += " and Cmmt_CmmtCode = '" & F09ProductWebCombo.SelectedRow.Cells(2).Value & "'"
                Session("SearchTxt") += " and Product_ProductCode = '" & F09ProductWebCombo.Value & "'"
                Session("SearchTxt") += " and Product_PlanYear = " & PlanYearsTxt.Text
            Else
                Alert("กรุณาเลือกผลิตภัณฑ์")
                Exit Sub
            End If
        ElseIf TabContainer1.ActiveTab.ID = TabPanel3.ID Then
            If F10ProductWebCombo.Value <> "" Then
                'Session("SearchTxt") += " and Cmmt_CmmtCode = '" & F10ProductWebCombo.SelectedRow.Cells(2).Value & "'"
                Session("SearchTxt") += " and Product_ProductCode = '" & F10ProductWebCombo.Value & "'"
                Session("SearchTxt") += " and Product_PlanYear = " & PlanYearsTxt.Text
            Else
                Alert("กรุณาเลือกผลิตภัณฑ์")
                Exit Sub
            End If
        End If


        'If ProductWebCombo.Value <> "" Then
        '    Session("SearchTxt") += " and Product_ProductCode = '" & ProductWebCombo.Value & "'"
        'End If

        'sql += " and Product_PlanYear = " & PlanYearsTxt.Text

        Session("PlanYearTxt") = PlanYearsTxt.Text
        'MsgBox(SearchTxt.Text)
        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
        t_countdata.Visible = True
        If Val(Session("count_data")) > 0 Then
            L_CountData.Text = FormatNumber(Val(Session("count_data")), 0) & " รายการ"
        Else
            L_CountData.Text = "ไม่พบข้อมูลที่ค้นหา"
        End If
    End Sub

    Protected Sub CommtItemCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles CommtItemCombo.InitializeDataSource

        Dim adap As New MasterDataSet2TableAdapters.CmmtsTableAdapter
        Dim dt As New MasterDataSet2.CmmtsDataTable
        dt = adap.GetDataByPlanYear2(Session("PlanYearTxt"))
        e.DataSource = dt

    End Sub

    'Protected Sub ProductWebCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs)

    '    Dim adap As New MasterDataSetTableAdapters.ProductsTableAdapter
    '    Dim dt As New MasterDataSet.ProductsDataTable
    '    dt = adap.GetDataByPlanYear(Session("PlanYearTxt"))
    '    e.DataSource = dt
    'End Sub

    Protected Sub F09ProductWebCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs)

        Dim adap As New MasterDataSetTableAdapters.ProductsTableAdapter
        Dim dt As New MasterDataSet.ProductsDataTable
        dt = adap.GetDataByF09(Session("PlanYearTxt"))
        e.DataSource = dt
    End Sub

    Protected Sub F10ProductWebCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs)

        Dim adap As New MasterDataSetTableAdapters.ProductsTableAdapter
        Dim dt As New MasterDataSet.ProductsDataTable
        dt = adap.GetDataByF10(Session("PlanYearTxt"))
        e.DataSource = dt
    End Sub

    Function GetShowDes(ByVal id As Object) As String
        If IsDBNull(id) = True Then
            Return Nothing
        End If

        Dim eleText As String = ""
        Dim adp As New MasterDataSetTableAdapters.CmmtDetailsTableAdapter
        Dim dt As New MasterDataSet.CmmtDetailsDataTable
        dt = adp.GetDataByID(id)

        For Each row As MasterDataSet.CmmtDetailsRow In dt

            If row.IsElementDescription1Null = False And row.IsElementUnit1Null = False Then
                If row.ElementDescription1 <> "" And row.ElementUnit1 <> "" Then
                    eleText += row.ElementDescription1 & "-" & row.ElementUnit1 & ","
                End If
            End If
            If row.IsElementDescription2Null = False And row.IsElementUnit2Null = False Then
                If row.ElementDescription2 <> "" And row.ElementUnit2 <> "" Then
                    eleText += row.ElementDescription2 & "-" & row.ElementUnit2 & ","
                End If
            End If
            If row.IsElementDescription3Null = False And row.IsElementUnit3Null = False Then
                If row.ElementDescription3 <> "" And row.ElementUnit3 <> "" Then
                    eleText += row.ElementDescription3 & "-" & row.ElementUnit3 & ","
                End If
            End If
            If row.IsElementDescription4Null = False And row.IsElementUnit4Null = False Then
                If row.ElementDescription4 <> "" And row.ElementUnit4 <> "" Then
                    eleText += row.ElementDescription4 & "-" & row.ElementUnit4 & ","
                End If
            End If
            If row.IsElementDescription5Null = False And row.IsElementUnit5Null = False Then
                If row.ElementDescription5 <> "" And row.ElementUnit5 <> "" Then
                    eleText += row.ElementDescription5 & "-" & row.ElementUnit5 & ","
                End If
            End If
            If row.IsElementDescription6Null = False And row.IsElementUnit6Null = False Then
                If row.ElementDescription6 <> "" And row.ElementUnit6 <> "" Then
                    eleText += row.ElementDescription6 & "-" & row.ElementUnit6 & ","
                End If
            End If
            If row.IsElementDescription7Null = False And row.IsElementUnit7Null = False Then
                If row.ElementDescription7 <> "" And row.ElementUnit7 <> "" Then
                    eleText += row.ElementDescription7 & "-" & row.ElementUnit7 & ","
                End If
            End If
            If row.IsElementDescription8Null = False And row.IsElementUnit8Null = False Then
                If row.ElementDescription8 <> "" And row.ElementUnit8 <> "" Then
                    eleText += row.ElementDescription8 & "-" & row.ElementUnit8 & ","
                End If
            End If
            If row.IsElementDescription9Null = False And row.IsElementUnit9Null = False Then
                If row.ElementDescription9 <> "" And row.ElementUnit9 <> "" Then
                    eleText += row.ElementDescription9 & "-" & row.ElementUnit9 & ","
                End If
            End If
        Next

        Dim Myele As String
        If eleText.Length > 0 Then
            Myele = eleText.Remove(eleText.Length - 1, 1)
        Else
            Myele = ""
        End If


        If Myele <> "" Then
            Return Myele
        Else
            Return "-"
        End If
    End Function
    Function getEnterFlag(ByVal id As Decimal) As String
        Dim Flag As String = ""
        Dim result As String = ""
        Dim adp As New MasterDataSetTableAdapters.CmmtDetailsTableAdapter
        Flag = adp.GetEnterAmountFlagByID(id)

        If Flag = "Y" Then
            result = "ได้"
        ElseIf Flag = "N" Or Val(Flag) = 0 Then
            result = "ไม่ได้"
        End If

        Return result
    End Function

    'Protected Sub AddBtn_Click(sender As Object, e As ImageClickEventArgs) Handles AddBtn.Click
    '    If CommtItemCombo.Value = "" Then
    '        Alert("กรุณาเลือก Commitment Item")
    '        Exit Sub
    '    End If
    '    ScriptText = "<script>popupCMDetailnew(); return false;</script>"
    'End Sub
    Sub Alert(ByVal message As String)
        AlertText = "<script> alert('" & message & "');</script>"
    End Sub

    Protected Sub SaveBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SaveBtn.Click
        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
    End Sub

End Class
